﻿<%@ WebHandler Language="C#" Class="Measurement" %>

using System;
using System.Web;
using System.Xml;
using System.Xml.XPath;

public class Measurement : IHttpHandler
{
    public static XmlDocument xmlContent;
    public void ProcessRequest(HttpContext context)
    {
        
        string id = HttpContext.Current.Request.Form["id"];//.QueryString["id"];
        context.Response.ContentType = "text/xml";
        string fileName = HttpContext.Current.Server.MapPath(Constant.XmlFileName);
        XPathDocument doc = new XPathDocument(fileName);
        string automationPath = "//Automation[@id]";
        var nav = doc.CreateNavigator();
        var matchedElement = nav.Select(automationPath);
        while (matchedElement.MoveNext())
        {
            if( matchedElement.Current.GetAttribute( "id", "" ) == id )
            {
               if( matchedElement.Current.MoveToChild("Measurement", "") )
               {
                   context.Response.Write(matchedElement.Current.OuterXml);
                   return;
               }
            }
        }
        return;
        
        
        XmlDocument xmlDoc = new XmlDocument();
        if (string.IsNullOrEmpty( id ))
        {
            if (xmlContent == null)
            {
                xmlContent = new XmlDocument();
                //2010-05-30 10:00:00
                xmlContent.LoadXml(@"<Measurement>
      <Alert>
        <Content>EIA Petrol A</Content>
        <Time>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + @"</Time>
        <Competitor>BBG</Competitor>
        <CompetitorTime>2010-05-30 10:00:00</CompetitorTime>
        <Result>Draw</Result>
      </Alert>
      <Alert>
        <Content>EIA Petrol B</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>BBG</Competitor>
        <CompetitorTime>2010-05-30 10:00:03</CompetitorTime>
        <Result>Win</Result>
      </Alert>
      <Alert>
        <Content>EIA Petrol K</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>BBG</Competitor>
        <CompetitorTime>2010-05-30 10:00:00</CompetitorTime>
        <Result>Draw</Result>
      </Alert>
      <Alert>
        <Content>EIA Petrol C</Content>
        <Time>2010-05-30 10:00:03</Time>
        <Competitor>BBG</Competitor>
        <CompetitorTime>2010-05-30 10:00:00</CompetitorTime>
        <Result>Loss</Result>
      </Alert>
      <Alert>
        <Content>EIA Petrol D</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>DJN</Competitor>
        <CompetitorTime>2010-05-30 10:00:00</CompetitorTime>
        <Result>Draw</Result>
      </Alert>
      <Alert>
        <Content>EIA Petrol G</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>DJN</Competitor>
        <CompetitorTime>2010-05-30 10:00:03</CompetitorTime>
        <Result>Win</Result>
      </Alert>
      <Alert>
        <Content>EIA Petrol E</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>DJN</Competitor>
        <CompetitorTime>2010-05-30 10:00:05</CompetitorTime>
        <Result>Win</Result>
      </Alert>
    </Measurement>");
            }
            else
            {
                XmlDocument newXmlDoc = new XmlDocument();
                newXmlDoc.LoadXml(string.Format( @"<Alert>
        <Content>EIA Petrol {0}</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>DJN</Competitor>
        <CompetitorTime>{1}</CompetitorTime>
        <Result>Win</Result>
      </Alert>", Guid.NewGuid().ToString(), DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" )));
                xmlContent.FirstChild.RemoveAll();
                xmlContent.FirstChild.AppendChild(xmlContent.ImportNode(newXmlDoc.DocumentElement, true));
            }
        }
        else
        {
            xmlContent = new XmlDocument();
            xmlContent.LoadXml(@"<Measurement>
      <Alert>
        <Content>EIA Nat Gas A</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>BBG</Competitor>
        <CompetitorTime>2010-05-30 10:00:00</CompetitorTime>
        <Result>Draw</Result>
      </Alert>
      <Alert>
        <Content>EIA Nat Gas B</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>BBG</Competitor>
        <CompetitorTime>2010-05-30 10:00:03</CompetitorTime>
        <Result>Win</Result>
      </Alert>
      <Alert>
        <Content>EIA Nat Gas C</Content>
        <Time>2010-05-30 10:00:03</Time>
        <Competitor>BBG</Competitor>
        <CompetitorTime>2010-05-30 10:00:00</CompetitorTime>
        <Result>Loss</Result>
      </Alert>
      <Alert>
        <Content>EIA Nat Gas D</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>DJN</Competitor>
        <CompetitorTime>2010-05-30 10:00:00</CompetitorTime>
        <Result>Draw</Result>
      </Alert>
      <Alert>
        <Content>EIA Nat Gas E</Content>
        <Time>2010-05-30 10:00:00</Time>
        <Competitor>DJN</Competitor>
        <CompetitorTime>2010-05-30 10:00:05</CompetitorTime>
        <Result>Win</Result>
      </Alert>
    </Measurement>");
        }
        context.Response.Write(xmlContent.OuterXml);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}